<template>
  <!-- 首页 -->
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="header-left">
        <img src="../assets/logo.png" alt="" />
        <span>通用后台管理系统</span>
      </div>
      <div class="header-right">
        <el-button type="info">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <!-- 左边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 展开收起按钮 -->
        <div class="collapseBtn" @click="changeCollapse">
          {{ isCollapse ? ">" : "<" }}
        </div>
        <!-- 侧边导航区 -->
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <!-- index确保唯一性，必须是字符串 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + itemchildren.path"
              v-for="itemchildren in item.children"
              :key="itemchildren.id"
              @click="saveNavPath('/' + itemchildren.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ itemchildren.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主内容 -->
      <el-main>
        <!-- 访问home页面所显示的路由 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "MyHome",
  components: "",
  data() {
    return {
      // 左侧菜单列表
      menuList: [],
      // 一级菜单对应字体图标
      iconList: {
        125: "iconfont icon-zhanghao",
        103: "iconfont icon-zhengfangti-mianxing",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-dingdandingdanmingxishouzhimingxi",
        145: "iconfont icon-shujutongji",
      },
      isCollapse: false,
      // 被激活列表的地址
      activePath: "",
    };
  },
  created() {
    // 页面创建完后直接请求侧边栏数据
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  mounted() {},
  methods: {
    // 获取侧边栏菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    // 控制侧边栏展开收起
    changeCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存列表激活状态
    saveNavPath(activepath) {
      window.sessionStorage.setItem("activePath", activepath);
      this.activePath = activepath;
    },
  },
};
</script>

<style scoped lang="less">
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  color: #fff;
  .header-left {
    display: flex;
    img {
      box-sizing: border-box;
    }
    span {
      font-size: 20px;
      align-self: center;
      margin-left: 15px;
    }
  }
  .header-right {
    display: flex;
    .el-button {
      align-self: center;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
  // 展开收起按钮
  .collapseBtn {
    color: #fff;
    text-align: end;
    line-height: 24px;
    margin-right: 5px;
    font-size: 24px;
    font-weight: 700;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
</style>
